﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace genQiang
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }
        Queue<long> his = new Queue<long>();

        int MAX_NUM = 15;
        int BOX_SIZE = 40;
        int YOU_XIAO = 1000; //规定时间内点击成功才算有效
        int num = 0;
        bool isRun = false;

        int window_w;
        int window_h;

        int caidan_h;

        long sroce;
        private void init()
        {
            isRun = false;
            this.Width = 720;
            this.Height = 580;
            pictureBox1.Width = pictureBox1.Height = BOX_SIZE;
            pictureBox1.Visible = false;
            num = 0;
            开始ToolStripMenuItem.Enabled = true;
            label1.Left = 0;
            sroce = 0;
            caidan_h= 开始ToolStripMenuItem.Height;
            label1.Top = caidan_h;
            this.FormBorderStyle = FormBorderStyle.FixedSingle;
            window_h = this.Height;
            window_w = this.Width;
            this.MaximizeBox = false;
        }
        private void Form3_Load(object sender, EventArgs e)
        {
            init();
            this.StartPosition = FormStartPosition.CenterParent;
        }

        Random random = new Random();
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (isRun && !pictureBox1.Visible)
            {
                int x=random.Next(0, window_w-BOX_SIZE-20);
                int y = random.Next(caidan_h+10, window_h- BOX_SIZE-40);
                pictureBox1.Left = x;
                pictureBox1.Top = y;
                pictureBox1.Visible = true; 
                shen = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds();
            }
            label1.Text = "分数:" + sroce.ToString() + " 剩余目标:" + (MAX_NUM - num).ToString() +";";
        }


        private void Form3_FormClosed(object sender, FormClosedEventArgs e)
        {
            MainWin.mainWin.Visible = true;
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        
        private void 开始ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            isRun = true;
            开始ToolStripMenuItem.Enabled = false;
           // dian = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds();
        }


        long shen;
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            num++;
            long dian = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds();
            long dt = dian - shen;
            Console.WriteLine(dt.ToString());
            if (YOU_XIAO - dt > 0) sroce += (YOU_XIAO - dt);
            if (num == MAX_NUM)
            {
                //统计
                DialogResult dialogResult = MessageBox.Show("分数:"+sroce.ToString()+ " 是否上传你的分数?","",MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (his.Count >= 10) 
                    his.Dequeue();
                his.Enqueue(sroce);
                long sroce2 = sroce;
                init();

                if (dialogResult == DialogResult.Yes)
                {
                    string httpresp = HttpUtils.HttpGet("cmt/" + MainWin.ZHCS + "/" + sroce2.ToString() + "/" + MainWin.youName);
                    MessageBox.Show(httpresp);
                }
            }
            pictureBox1.Visible = false;
        }

        private void 说明ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("PS\r\n以最快的速度瞄准+点击,总计" + MAX_NUM.ToString() + "个目标\r\n得分规则:单个目标" + YOU_XIAO +"ms內点击完毕可以获得分数\r\n分数为(1000ms-单次你所花费的时间)分", "说明", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        private void 历史分数ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string msg = "历史得分:\r\n";
            if (his.Count == 0)
            {
                MessageBox.Show("暂无历史成绩", "历史成绩", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            long avg = 0;
            foreach (var i in his.Take(his.Count).Reverse())
            {
                avg += i;
                msg += (i.ToString() + "\r\n");
            }
            msg += ("平均分:" + (avg / his.Count).ToString() + "");
            MessageBox.Show(msg, "历史成绩");
        }
    }
}
